package meta

import (
	"engine/log"
	"gitee.com/ameise84/e3time"
	"servers/models/define"
)

type Rtp struct {
	RTP         define.RTPValue
	Level       define.CtrlLevelType
	RateSelect  int
	SelectLevel define.CtrlLevelType
}

func (v *Rtp) FromSlice(s []int64) {
	v.RTP = s[0]
	v.Level = define.CtrlLevelType(s[1])
	v.RateSelect = int(s[2])
	v.SelectLevel = define.CtrlLevelType(s[3])
}

type RtpArray struct {
	Values []Rtp
}

func (v *RtpArray) FromSlice(s [][]int64) {
	v.Values = make([]Rtp, len(s))
	for i, vs := range s {
		v.Values[i].FromSlice(vs)
	}
	for i := 1; i < len(v.Values); i++ {
		if v.Values[i].RTP >= v.Values[i-1].RTP {
			log.GetLogger().Fatal("rtp cfg error").Println()
		}
	}
}

type RtpRestore struct {
	Weight int
	MinRTP define.RTPValue
	MaxRTP define.RTPValue
}

func (v *RtpRestore) FromSlice(s []int64) {
	v.Weight = int(s[0])
	v.MinRTP = s[1]
	v.MaxRTP = s[2]
}

type RtpRestoreArray struct {
	TotalWeight int
	Ls          []RtpRestore
}

func (v *RtpRestoreArray) FromSlice(s [][]int64) {
	v.Ls = make([]RtpRestore, len(s))
	for i, vs := range s {
		v.Ls[i].FromSlice(vs)
		v.TotalWeight += v.Ls[i].Weight
	}
}

type RptInfo struct {
	TotalIn    define.GameScore   `json:"ti,omitempty"`
	TotalOut   define.GameScore   `json:"to,omitempty"`
	DailyIn    define.GameScore   `json:"di,omitempty"`
	DailyOut   define.GameScore   `json:"do,omitempty"`
	RoundIns   []define.GameScore `json:"-"`
	RoundOuts  []define.GameScore `json:"-"`
	RoundIdx   int                `json:"-"`
	RoundIn    define.GameScore   `json:"-"`
	RoundOut   define.GameScore   `json:"-"`
	LastUpdate int64              `json:"up,omitempty"`
}

func (info *RptInfo) Update(inScore, outScore define.GameScore, timestamp int64) {
	now := e3time.Unix(timestamp, 0)
	last := e3time.Unix(info.LastUpdate, 0)

	if !e3time.IsSameDay(now, last) {
		info.DailyIn = 0
		info.DailyOut = 0
	}

	info.TotalIn += inScore
	info.TotalOut += outScore

	info.DailyIn += inScore
	info.DailyOut += outScore

	info.RoundIn += inScore - info.RoundIns[info.RoundIdx]
	info.RoundOut += outScore - info.RoundOuts[info.RoundIdx]
	info.RoundIns[info.RoundIdx] = inScore
	info.RoundOuts[info.RoundIdx] = outScore
	info.RoundIdx = (info.RoundIdx + 1) % len(info.RoundIns)
	info.LastUpdate = timestamp
}

func (info *RptInfo) InitRoundRtp(length int) {
	info.RoundIns = make([]define.GameScore, length)
	info.RoundOuts = make([]define.GameScore, length)
}
